package co.yixiang.controller.buyer.monitoring;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.annotation.Log;
import co.yixiang.common.enums.SortEnum;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.monitoring.entity.domain.Config;
import co.yixiang.modules.monitoring.entity.query.YxMonitoringQueryCriteria;
import co.yixiang.modules.monitoring.entity.vo.YxMonitoringCategoryVo;
import co.yixiang.modules.monitoring.entity.vo.YxMonitoringSmallVo;
import co.yixiang.modules.monitoring.fluorite.entity.FluoriteQueryForm;
import co.yixiang.modules.monitoring.fluorite.entity.dto.DeviceLiveDto;
import co.yixiang.modules.monitoring.fluorite.entity.dto.DeviceLiveV2Dto;
import co.yixiang.modules.monitoring.fluorite.service.FluoriteService;
import co.yixiang.modules.monitoring.service.YxMonitoringCategoryService;
import co.yixiang.modules.monitoring.service.YxMonitoringService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "商城:监控管理")
@RestController
@RequestMapping("/monitor")
public class MonitoringBuyerController {
    @Autowired
    private YxMonitoringCategoryService categoryService;
    @Autowired
    private YxMonitoringService monitoringService;
    @Autowired
    public Config config;// 配置文件bean

    @Autowired
    private FluoriteService fluoriteService;

    @ApiOperation("获取监控分类")
    @GetMapping(value = "/category")
    public ResultMessage<List<YxMonitoringCategoryVo>> monitoringCategorys() {
        List<YxMonitoringCategoryVo> yxMonitoringCategoryVos = categoryService.queryAllByVo();
        return ResultUtil.data(yxMonitoringCategoryVos);
    }

    @ApiOperation(value = "分页查询监控数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cateId", value = "分类ID", paramType = "query"),
            @ApiImplicitParam(name = "sortType", value = "排序类型:VISIT、DISTANCE", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "排序类型为DISTANCE,该字段必须有值", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "排序类型为DISTANCE,该字段必须有值", paramType = "query")
    })
    @GetMapping
    public ResultMessage<Map<String, Object>> getYxArticles(YxMonitoringQueryCriteria criteria, Pageable pageable) {
        // 销量 [默认] // 距离排序 // 人气
        Sort sort;
        switch (criteria.getSortType()) {
            case "VISIT":
                sort = Sort.by(Sort.Direction.DESC, criteria.getSortType());
                break;
            default:
                sort = Sort.by(Sort.Direction.DESC, SortEnum.SORT.name());

        }
        Pageable pageableT = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        final Map<String, Object> stringObjectMap = monitoringService.queryAllByCateId(criteria, pageableT);
        return ResultUtil.data(stringObjectMap);
    }

    @Log("监控详情")
    @ApiOperation(value = "监控详情")
    @GetMapping(value = "/detail/{id}/{channel}")
    public ResultMessage<YxMonitoringSmallVo> getYxStoreProductsDetail(@PathVariable Long id,@PathVariable String channel) {
        final YxMonitoringSmallVo yxMonitoringVo = monitoringService.queryMonitoringDetail(id, channel);
        return ResultUtil.data(yxMonitoringVo);
    }


    @Log("监控播放地址-新")
    @ApiOperation(value = "监控播放地址-新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceSerial", value = "设备序列号", paramType = "query"),
            @ApiImplicitParam(name = "channelNo", value = "通道号,，非必选，默认为1", paramType = "query"),
            @ApiImplicitParam(name = "protocol", value = "流播放协议，1-ezopen、2-hls、3-rtmp、4-flv，默认为1", paramType = "query"),
            @ApiImplicitParam(name = "quality", value = "视频清晰度，1-高清（主码流）、2-流畅（子码流）", paramType = "query")
    })
    @PostMapping(value = "/v2/live/address")
    public ResultMessage<DeviceLiveDto> getV2LiveAddress(@RequestBody FluoriteQueryForm form) {
        final DeviceLiveDto deviceLiveDto = fluoriteService.liveAddressV2(form);
        return ResultUtil.data(deviceLiveDto);
    }

    @Log("监控播放地址-旧")
    @ApiOperation(value = "监控播放地址-旧")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceSerial", value = "设备序列号", paramType = "query"),
            @ApiImplicitParam(name = "source", value = "直播源，[设备序列号]:[通道号],[设备序列号]:[通道号]的形式,默认第一个通道", paramType = "query")
    })
    @PostMapping(value = "/live/address")
    public ResultMessage<DeviceLiveV2Dto> getLiveAddress(@RequestBody FluoriteQueryForm form) {
        if (ObjectUtil.isEmpty(form.getSource())) {
            form.setSource(form.getDeviceSerial()+":1");
        }
        DeviceLiveV2Dto deviceLiveV2Dto = fluoriteService.liveAddress(form);
        return ResultUtil.data(deviceLiveV2Dto);
    }
    @Log("萤石云AccessToken")
    @ApiOperation(value = "萤石云AccessToken")
    @GetMapping(value = "/token")
    public ResultMessage<String> fluorteToken() {
        final String accessToken = fluoriteService.getAccessToken();
        return ResultUtil.data(accessToken);
    }


}
